package com.yizhubao.app.utils.net;

import com.yizhubao.app.utils.Constants;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import h.framework.pacy.utils.AppUtil;
import h.framework.pacy.utils.PreferencesUtil;
import h.framework.pacy.utils.StringUtil;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @author PACY
 * 创建时间 19/1/22 17:57.
 * 邮箱 pengwwe457621023@qq.com
 * 类描述   构造公共参数工具类
 */
public class HttpUtils {
    //当前版本号
    public static String version = AppUtil.getVersionCode() + "";
    //平台
    public static String os = "Android";


    /**
     * 构建参数
     *
     * @param params 除公共参数以外的具体参数
     * @return
     */
    public static Map<String, Object> buildParams(Map<String, Object> params) {
        Map<String, Object> allParams = new HashMap<>();

//        allParams.put("package_id", EnvConfig.CHANNEL_ID);
        //如果token不为空则添加token到参数列表
        if (StringUtil.isNotEmpty((String) PreferencesUtil.get(Constants.USER_ID, ""))) {
            allParams.put("userId", PreferencesUtil.get(Constants.USER_ID, ""));
        }
        if (params != null) {
            allParams.putAll(params);
        }
        return allParams;
    }


    /**
     * 构建form参数  使用表单上传  aoligei
     *
     * @param params
     * @return
     */
    public static RequestBody buildBody(Map<String, String> params) {

        Map<String, String> allParams = new HashMap<>();
        allParams.put("userId","1");
//        allParams.put("channelId", EnvConfig.CHANNEL_ID);
        //如果token不为空则添加token到参数列表
//        if (StringUtil.isNotEmpty((String) PreferencesUtil.get(Constans.AUTHORIZATION, ""))) {
////            allParams.put("Authorization", "bearer "+PreferencesUtil.get(Constans.AUTHORIZATION, ""));
////        }
        if (params != null) {
            allParams.putAll(params);
        }

        MultipartBody.Builder builder = new MultipartBody.Builder()
            .setType(MultipartBody.FORM);

        for (Map.Entry<String, String> entry : allParams.entrySet()) {
            if (entry.getValue() != null) {
                builder.addFormDataPart(entry.getKey(), entry.getValue());
            }
        }
        RequestBody requestBody = builder.build();

        return requestBody;
    }


    /**
     * 构建form参数  使用表单上传
     *
     * @param params
     * @return requestbody集合方式  暂未使用
     */
    public static Map<String, RequestBody> buildBodys(Map<String, Object> params) {

        Map<String, Object> allParams = new HashMap<>();

//        allParams.put("package_id", EnvConfig.CHANNEL_ID);
        //如果token不为空则添加token到参数列表
        if (StringUtil.isNotEmpty((String) PreferencesUtil.get("token", ""))) {
            allParams.put("token", (String) PreferencesUtil.get("token", ""));
        }
        if (params != null) {
            allParams.putAll(params);
        }

        Map<String, RequestBody> requestBodyMap = new HashMap<>();
        for (String key : allParams.keySet()) {
            RequestBody requestBody = null;
            if ((allParams.get(key) == null ? "" : allParams.get(key)) instanceof String) {
                requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
                    allParams.get(key) == null ? "" : (String) allParams.get(key));
                requestBodyMap.put(key, requestBody);
            } else if ((allParams.get(key) == null ? "" : allParams.get(key)) instanceof File) {

                requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
                    (File) allParams.get(key));
                requestBodyMap.put(key + "\";filename=\""+ "headIcon.png", requestBody);
            }


        }
        return requestBodyMap;

    }
}
